package day16.edu;

import org.junit.Test;

// Created: 16/09/2025, ChangJiang Ru
public class StringTest {
    @Test
    public void test1()
    {
        String s = "abcdefg";
        String s1 = reverse(s, 2, 3);
        System.out.println(s1);
    }
    
    @Test
    public void test2()
    {
        String s = "grammar";
        String s1 = reverse1(s,1,4);
        System.out.println(s1);
    }

    public String reverse(String str, int fromIndex, int endIndex)
    {
        char[] arr = str.toCharArray();

        for(int i = fromIndex, j = endIndex; i < j; i++, j--) {
            char temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

        return new String(arr);
    }

    public String reverse1(String str, int fromIndex, int endIndex)
    {
        String finalString = str.substring(0, fromIndex);

        for(int i = endIndex; i >= fromIndex; i--)
        {
            finalString += str.charAt(i);
        }

        finalString += str.substring(endIndex + 1);
        
        return finalString;
    }

    /**
     * We will find the count of sub in subString
     * @Descprition "ab" "abkkcadkabkebfkabkskab"
     * @param sub
     * @param subString
     * @return
     */
    public int getSubStringCount(String sub, String subString)
    {
        int count = 0;
        int num = subString.length();

        for(int i = 0; i < sub.length() - subString.length() + 1; i++)
        {
            if(subString.equals(sub.substring(i, i + num)))
                count++;
        }

        return count;
    }

    public int getSubStringCount1(String sub, String subString)
    {
        int count = 0;
        int num = subString.length();

        for (int i = 0; i < sub.length() - num + 1; i++)
        {
            if(sub.indexOf(subString, i, i + num ) >= 0)
                count++;
        }

        return count;
    }

    public int getSubStringCount2(String sub, String subString)
    {
        int count = 0;
        int num = subString.length();

        for (int i = 0 ; i < sub.length() - num + 1; i++)
        {
            if(sub.substring(i, i + num).contains(subString))
                count++;
        }

        return count;
    }
    @Test
    public void test3()
    {
        String sub = "abkkcadkabkebfkabkskab";
        String subString = "abk";
        /*
        int index = sub.indexOf(subString, 0, sub.length() - 1);
        System.out.println(index);

         */

        System.out.println(getSubStringCount(sub, subString));
        System.out.println(getSubStringCount1(sub,subString));
        System.out.println(getSubStringCount2(sub, subString));
    }

    public int getSubStringCount3(String str, String subStr)
    {
        int count = 0;

        int index = str.indexOf(subStr);
        while(index >= 0)
        {
            count++;

            index = str.indexOf(subStr, index + subStr.length());
        }

        return count;
    }

    @Test
    public void test4()
    {
        String str = "abkkcadkabkebfkabkskab";
        String subStr = "abk";

        System.out.println(getSubStringCount3(str, subStr));
    }
}
